package com.qiall.omsserver.service.log;
import com.qiall.exceptions.ExceptionUtil;
import com.qiall.bashutil.event.CommandExecFailEvent;
import com.qiall.bashutil.event.CommandMessageEvent;
import com.qiall.bashutil.event.MvnPackageSuccessEvent;
import com.qiall.omsserver.data.entity.ApplicationInfo;
import com.qiall.omsserver.data.entity.OmsLogInfo;
import com.qiall.omsserver.data.mapper.ApplicationInfoMapper;
import com.qiall.omsserver.data.mapper.OmsLogInfoMapper;
import com.qiall.omsserver.event.PullFailEvent;
import com.qiall.omsserver.event.PullSuccessEvent;
import com.qiall.omsserver.service.OmsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class LogByEventServiceImpl implements OmsLogService {
    @Autowired
    private OmsLogInfoMapper omsLogInfoMapper;

    @Autowired
    private ApplicationInfoMapper applicationInfoMapper;

    @EventListener(classes = {PullSuccessEvent.class})
    public void listen(PullSuccessEvent event) {

        int appId = event.getAppId();
        ApplicationInfo applicationInfo = applicationInfoMapper.select(appId);
        String message = applicationInfo.getAppName() + " 代码拉取成功";
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }

    @EventListener(classes = {PullFailEvent.class})
    public void listen(PullFailEvent event) {

        int appId = event.getAppId();
        ApplicationInfo applicationInfo = applicationInfoMapper.select(appId);
        String message = applicationInfo.getAppName() + " 代码拉取失败";
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }

    @EventListener(classes = {CommandMessageEvent.class})
    public void listen(CommandMessageEvent commandMessageEvent) {
        String message = commandMessageEvent.getMessage();
        int appId = commandMessageEvent.getOutId();
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }

    @EventListener(classes = {CommandExecFailEvent.class})
    public void listen(CommandExecFailEvent failEvent) {
        Exception e = failEvent.getE();
        String message = ExceptionUtil.getExceptionMessage(e);
        int appId = failEvent.getOutId();
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }

    @EventListener(classes = {MvnPackageSuccessEvent.class})
    public void listen(MvnPackageSuccessEvent mvnPackageSuccessEvent) {
        int appId = mvnPackageSuccessEvent.getOutId();
        ApplicationInfo applicationInfo = applicationInfoMapper.select(appId);
        String message = applicationInfo.getAppName() + " 打包成功";
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }

    @Override
    public void omsLog(int appId, String message) {
        OmsLogInfo omsLogInfo = new OmsLogInfo();
        omsLogInfo.setInfo(message);
        omsLogInfo.setAppId(appId);
        omsLogInfoMapper.insert(omsLogInfo);
    }
}


